﻿using IcsSEMCtrl.Global.Device;
using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Service.Material;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Unit;
using Lifter.Global;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Panels;

namespace Lifter.Gui.Monitor.Popups
{
    public partial class MaterialAdd : UIEditForm
    {
        private LifterSEM lftSem = null;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000; // 用双缓冲绘制窗口的所有子控件
                return cp;
            }
        }
        public MaterialAdd()
        {
            InitializeComponent();
            if (this.lftSem == null)
            {
                this.lftSem = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            }
            this.LoadcbMaterialLocalData();
            this.LoadcbMaterialTypeData();
            this.LoadcbStatusData();
        }

        protected override bool CheckData()
        {
            return CheckEmpty(tbMaterialID, "请输入物料ID")
                   && CheckEmpty(cbMaterialLocal, "请输入物料当前位置");
        }
        /// <summary>
        /// 手动创建物料信息
        /// add by yangshl
        /// </summary>
        public async void CreateManualMaterial()
        {
            try
            {
                //判断输入的信息是否能创建物料
                string materialID = this.tbMaterialID.Text;
                string materialLocal = this.cbMaterialLocal.Text;
                string materialTypeText = this.cbMaterialType.Text;
                string materialStatusText = this.cbStatus.Text;
                
                Carrier carrierExist = this.lftSem.GetCarrierByCarrierID(materialID);

                //判断物料是否已经存在
                if (carrierExist != null)
                {
                    LogHelper.loggerInfo("在CreateManualMaterial方法，手动创建物料时，物料信息已存在，物料ID为:" + materialID);
                    this.ShowWarningDialog("在CreateManualMaterial方法，手动创建物料时，物料信息已存在，物料ID为:" + materialID);
                    LogHelper.operationLogger(string.Format("用户[{0}]创建物料失败，物料ID为:[{1}]，失败原因:物料信息已经存在", this.lftSem.UserName, materialID));
                    lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]创建物料失败，物料ID为:[{1}]，失败原因:物料信息已经存在", this.lftSem.UserName, materialID));
                    return;
                }

                CarrierState materialStatus = CarrierState.StringToValue(materialStatusText);
                CarrierType materialType = CarrierType.StringToValue(materialTypeText);

                this.lftSem.InstallMaterial(materialID, materialLocal, "", materialStatus, materialType, enumIDReadState.FAILURE.ToString(), Carrier.MaterialCreateTypeConstants.MANUAL);

                Carrier carrier = this.lftSem.GetCarrierByCarrierID(materialID);

                if (carrier != null)
                {
                    LocationUnit locationUnit = this.lftSem.GetLocationUnit(materialLocal);
                    if (locationUnit is LifterPort)
                    {
                        LifterPort lifterPort = (LifterPort)locationUnit;
                        lifterPort.CarrierId = carrier.CarrierId;
                        lifterPort.I_BUFFERMATERIALID = materialID;
                        await Task.Delay(200);
                        lifterPort.O_MANUALMATERIALID = true;
                        await Task.Delay(1000);
                        lifterPort.O_MANUALMATERIALID = false;
                    }
                    else if (locationUnit is ConveyorBuffer)
                    {
                        ConveyorBuffer conveyorBuffer = (ConveyorBuffer)locationUnit;
                        conveyorBuffer.CSTID = carrier.CarrierId;
                        conveyorBuffer.I_CVMATERIALID = materialID;
                        await Task.Delay(200);
                        conveyorBuffer.O_MANUALMATERIALID = true;
                        await Task.Delay(1000);
                        conveyorBuffer.O_MANUALMATERIALID = false;
                    }
                    else if (locationUnit is Vehicle)
                    {
                        Vehicle vehicle = (Vehicle)locationUnit;
                        vehicle.CarrierID = carrier.CarrierId;
                        vehicle.I_SHUTTLEMATERIALID = materialID;
                        await Task.Delay(200);
                        vehicle.O_MANUALMATERIALID = true;
                        await Task.Delay(1000);
                        vehicle.O_MANUALMATERIALID = false;
                    }
                    this.ShowSuccessDialog(string.Format("在{0}手动创建物料[{1}]成功", materialLocal, materialID));
                    LogHelper.loggerInfo(string.Format("在{0}手动创建物料[{1}]成功", materialLocal, materialID));
                    LogHelper.operationLogger(string.Format("用户[{0}]在{1}手动创建物料成功，物料ID为:[{2}]", this.lftSem.UserName, materialLocal, materialID));
                    lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]创建物料成功，物料ID为:[{1}]", this.lftSem.UserName, materialID));
                    MaterialInfoControl.materialInfoControl.UpdateDelegate();
                }
                else
                {
                    this.ShowWarningDialog("手动创建物料失败,物料信息不存在");
                    LogHelper.loggerInfo("手动创建物料失败，物料信息不存在");
                    LogHelper.operationLogger(string.Format("用户[{0}]创建物料失败，物料ID为:[{1}]", this.lftSem.UserName, materialID));
                    lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]创建物料失败，物料ID为:[{1}]", this.lftSem.UserName, materialID));
                }
            }
            catch (Exception ex)
            {
                LogHelper.loggerError("手动创建物料报错",ex);
            }
            
        }
        /// <summary>
        /// 加载物料当前位置控件数据
        /// </summary>
        private void LoadcbMaterialLocalData()
        {
            List<string> LocationIDList = new List<string>();
            //加载Port口ID
            foreach (LifterPort lifterPort in this.lftSem.LifterPorts.Values)
            {
                LocationIDList.Add(lifterPort.HostID);
            }
            //加载CV ID
            foreach (ConveyorBuffer conveyorBuffer in this.lftSem.ConveyorBuffers.Values)
            {
                LocationIDList.Add(conveyorBuffer.HostID);
            }
            //加载轿厢CV ID
            foreach (Vehicle vehicle in this.lftSem.Vehicles.Values)
            {
                LocationIDList.Add(vehicle.HostID);
            }
            this.cbMaterialLocal.Items.AddRange(LocationIDList.ToArray());
        }
        /// <summary>
        /// 加载物料类型控件数据
        /// </summary>
        private void LoadcbMaterialTypeData()
        {
            List<string> materialTypeList = new List<string>();

            materialTypeList.Add(CarrierType.displayStrings[2]);
            materialTypeList.Add(CarrierType.displayStrings[5]);
            materialTypeList.Add(CarrierType.displayStrings[6]);
            materialTypeList.Add(CarrierType.displayStrings[7]);

            this.cbMaterialType.Items.AddRange(materialTypeList.ToArray());
        }

        /// <summary>
        /// 加载物料状态控件数据
        /// </summary>
        private void LoadcbStatusData()
        {
            List<string> materialStatus = new List<string>();

            materialStatus.Add(CarrierState.displayStrings[0]);
            materialStatus.Add(CarrierState.displayStrings[1]);
            materialStatus.Add(CarrierState.displayStrings[2]);
            materialStatus.Add(CarrierState.displayStrings[5]);
            materialStatus.Add(CarrierState.displayStrings[6]);

            this.cbStatus.Items.AddRange(materialStatus.ToArray());
        }
    }
}